package com.gmail.fanvaron.Main;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.bukkit.configuration.file.FileConfiguration;
import org.bukkit.configuration.file.YamlConfiguration;
import org.bukkit.plugin.java.JavaPlugin;

import com.gmail.fanvaron.Commands.BuyCommand;
import com.gmail.fanvaron.Commands.RefillCommand;
import com.gmail.fanvaron.Commands.WarpCommand;
import com.gmail.fanvaron.Listener.BlockListener;
import com.gmail.fanvaron.Listener.DispenseListener;

public final class MarvMonsterPack extends JavaPlugin {

    private FileConfiguration refillListConfig = null;
    private File              refillListFile   = null;

    @Override
    public void onEnable() {
        getLogger().info("MarvMonsterPack gestartet!");
        // Infinite Dispenser & Sign
        getServer().getPluginManager().registerEvents(new DispenseListener(this), this);
        getServer().getPluginManager().registerEvents(new BlockListener(this), this);
        // Small World Protector
        // getServer().getPluginManager().registerEvents(new
        // BlockBreakListener(this), this);
        // getServer().getPluginManager().registerEvents(new
        // InteractListener(this), this);
        // No Hunger
        // getServer().getPluginManager().registerEvents(new
        // FoodChangeListener(this), this);
        // No Lightning
        // getServer().getPluginManager().registerEvents(new
        // BlockIgniteListener(this), this);

        getCommand("refill").setExecutor(new RefillCommand(this));
        getCommand("buy").setExecutor(new BuyCommand(this));
         getCommand("warp").setExecutor(new WarpCommand(this));
        // getCommand("golist").setExecutor(new GoCommand(this));
        // getCommand("secret").setExecutor(new SecretCommand(this));
        // getCommand("scicken").setExecutor(new ChickenScoreCommand(this));
        // getCommand("scow").setExecutor(new CowScoreCommand(this));
    }

    public void reloadRefillListConfig() {
        if (refillListFile == null) {
            refillListFile = new File(getDataFolder(), "RefillList.yml");
        }
        refillListConfig = YamlConfiguration.loadConfiguration(refillListFile);

        // Schaut nach den Standardwerten in der jar
        InputStream defConfigStream = getResource("RefillList.yml");
        if (defConfigStream != null) {
            YamlConfiguration defConfig = YamlConfiguration.loadConfiguration(defConfigStream);
            refillListConfig.setDefaults(defConfig);
        }
    }

    public FileConfiguration getRefillListConfig() {
        if (refillListConfig == null) {
            reloadRefillListConfig();
        }
        return refillListConfig;
    }

    public void saveRefillConfig() {
        if (refillListConfig == null || refillListFile == null) {
            return;
        }
        try {
            refillListConfig.save(refillListFile);
        } catch (IOException ex) {
            Logger.getLogger(JavaPlugin.class.getName()).log(Level.SEVERE,
                    "Refill Liste konnte nicht nach " + refillListFile + " geschrieben werden.", ex);
        }
    }

}